#!/usr/bin/env python3
# -*- coding:utf-8 -*-

# https://blog.csdn.net/belldeep/article/details/69186671
# python : turtle 画一朵花

# -*- coding: cp936 -*-
import turtle
import math


def p_line(printer, n, length, angle):
    """Draws n line segments."""
    for i in range(n):
        printer.fd(length)
        printer.lt(angle)


def polygon(printer, n, length):
    """Draws a polygon with n sides."""
    angle = 360 / n
    p_line(printer, n, length, angle)


def arc(printer, r, angle):
    """Draws an arc with the given radius and angle."""
    arc_length = 2 * math.pi * r * abs(angle) / 360
    n = int(arc_length / 4) + 1
    step_length = arc_length / n
    step_angle = float(angle) / n

    # Before starting reduces, making a slight left turn.
    printer.lt(step_angle / 2)
    p_line(printer, n, step_length, step_angle)
    printer.rt(step_angle / 2)


def petal(printer, r, angle):
    """Draws a 花瓣 using two arcs."""
    for i in range(2):
        arc(printer, r, angle)
        printer.lt(180 - angle)


def flower(printer, n, r, angle, p):
    """Draws a flower with n petals."""
    for i in range(n):
        petal(printer, r, angle)
        printer.lt(p / n)


def leaf(printer, r, angle, p):
    """Draws a 叶子 and fill it."""
    printer.begin_fill()  # Begin the fill process.
    printer.down()
    flower(printer, 1, r, angle, p)
    printer.end_fill()


def main():
    window = turtle.Screen()  # creat a screen
    window.bgcolor("white")
    window.title("draw a flower")
    printer = turtle.Turtle()
    printer.shape("turtle")
    printer.color("red")
    printer.width(3)
    # printer.speed(10)

    # Drawing flower
    flower(printer, 7, 60, 100, 360)

    # Drawing pedicel
    printer.color("brown")
    printer.rt(90)
    printer.fd(200)

    # Drawing leaf 1
    printer.width(1)
    printer.rt(270)
    printer.color("green")
    leaf(printer, 40, 80, 180)
    printer.rt(140)
    printer.color("black")
    printer.fd(30)
    printer.lt(180)
    printer.fd(30)

    # Drawing leaf 2
    printer.rt(120)
    printer.color("green")
    leaf(printer, 40, 80, 180)
    printer.color("black")
    printer.rt(140)
    printer.fd(30)
    printer.ht()  # hideturtle
    window.exitonclick()


if __name__ == "__main__":
    main()
